/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package TextNor.AssignTag;


import LM.LetterLanguageModel;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.XMLStreamWriter;

/**
 *
 * @author Administrator
 */
public final class AlphaTag {

    private LetterLanguageModel llmAswd=new LetterLanguageModel();
    private LetterLanguageModel llmSeqn=new LetterLanguageModel();
    private LetterLanguageModel llmExnp=new LetterLanguageModel();
    private int total=0;
    private double pAswd=0;
    private double pExpn=0;
    private double pSeqn=0;
    private ArrayList greekAr=new  ArrayList();
    private ArrayList wordAr=new ArrayList();
  
//    public static void main(String argv[]) throws FileNotFoundException, XMLStreamException, UnsupportedEncodingException, IOException {
//        AlphaTag at=new AlphaTag();
//        at.taging("test2_punc_nsw_url_split.num.xml", "test2_punc_nsw_url_split.num.alpha.xml");
//
//    }
    public AlphaTag(String inf, String ouf) throws FileNotFoundException, UnsupportedEncodingException, IOException, XMLStreamException {

        System.out.println("package TextNor.AssignTag; class AlphaTag");

        setGreek();
        setWordAr();

        llmAswd.loadModel("data\\llm\\llmAswd.db");
        llmSeqn.loadModel("data\\llm\\llmSeqn.db");
        llmExnp.loadModel("data\\llm\\llmExpn.db");
        int aswdNum=llmAswd.getTotal();
        int expnNum=llmExnp.getTotal();
        int seqnNum=llmSeqn.getTotal();
        total=aswdNum+expnNum+seqnNum;
        pAswd=(double)aswdNum/(double)total;
        pExpn=(double)expnNum/(double)total;
        pSeqn=(double)seqnNum/(double)total;
        taging(inf, ouf);
    }

    public void taging(String inf, String ouf) throws FileNotFoundException, XMLStreamException, UnsupportedEncodingException, IOException {
        long startTime = System.currentTimeMillis();
        int aswd=0;
        int expn=0;
        int seqn=0;
        FileInputStream fis = new FileInputStream(inf);
        XMLStreamReader xmlStreamReader = XMLInputFactory.newInstance().createXMLStreamReader(fis);

        FileOutputStream fos = new FileOutputStream(ouf);
        OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF8");
        XMLStreamWriter xmlStreamWriter = XMLOutputFactory.newInstance().createXMLStreamWriter(osw);

        xmlStreamWriter.writeStartElement("root");


        while (xmlStreamReader.hasNext()) {
            int eventCode = xmlStreamReader.next();
            switch (eventCode) {

                case XMLStreamConstants.START_ELEMENT:
                    String localName = xmlStreamReader.getLocalName();
                    if (localName.equals("record")) {
                        String RecId = xmlStreamReader.getAttributeValue(0);
                        xmlStreamWriter.writeStartElement(localName);
                        xmlStreamWriter.writeAttribute("RecId", RecId);

                    }

                    if (localName.equals("URL")) {
                        String url = xmlStreamReader.getElementText();

                        xmlStreamWriter.writeStartElement(localName);
                        xmlStreamWriter.writeCharacters(url);
                        xmlStreamWriter.writeEndElement();

                    }
                    if (localName.equals("Infomation")) {
                        xmlStreamWriter.writeStartElement(localName);
                    }
                    if (localName.equals("W")) {

                        String atVal = xmlStreamReader.getAttributeValue(0);
                        if (atVal.equals("ALPHA")) {
                            String nsw = xmlStreamReader.getElementText();
                            String nswLower=nsw.toLowerCase();
                            if(isWord(nswLower)){
                                
                                xmlStreamWriter.writeCharacters(nsw + " ");
                            }else{
                                String newAtVal = guessAlphaTag(nsw);

                            xmlStreamWriter.writeStartElement(localName);
                            xmlStreamWriter.writeAttribute("NSW", newAtVal);
                            xmlStreamWriter.writeCharacters(nsw);
                            xmlStreamWriter.writeEndElement();
                            
                            if(newAtVal.equals("ASWD")){
                                    aswd++;
                                }else{
                                    if(newAtVal.equals("EXPN")){
                                        expn++;
                                    }else{
                                        seqn++;
                                    }
                                }
                            }
                            

                        } else {
                            xmlStreamWriter.writeStartElement(localName);
                            xmlStreamWriter.writeAttribute("NSW", atVal);
                            String nsw = xmlStreamReader.getElementText();
                            xmlStreamWriter.writeCharacters(nsw);
                            xmlStreamWriter.writeEndElement();
                        }


                    }

                    if (localName.equals("SPLIT")) {
                        xmlStreamWriter.writeStartElement(localName);
                    }


                    break;

                case XMLStreamConstants.END_ELEMENT:
                    String endLocalName = xmlStreamReader.getLocalName();
                    //System.out.println(endLocalName);
                    if (endLocalName.equals("record")) {
                        xmlStreamWriter.writeEndElement();

                    }
                    if (endLocalName.equals("Infomation")) {
                        xmlStreamWriter.writeEndElement();

                    }

                    if (endLocalName.equals("SPLIT")) {
                        xmlStreamWriter.writeEndElement();
                    }



                    break;
                case XMLStreamConstants.CHARACTERS:
                    String character = xmlStreamReader.getText();
                    xmlStreamWriter.writeCharacters(character);
                    break;

                default:
                    break;
            }
        }


        xmlStreamWriter.writeEndElement();
        System.out.println("    +aswd: "+aswd);
        System.out.println("    +expn: "+expn);
        System.out.println("    +seqn: "+seqn);
        xmlStreamReader.close();
        xmlStreamWriter.close();
        fis.close();
        fos.close();
    }

    public String guessAlphaTag(String nsw) {
        if(isGreekNumber(nsw)){
            return "GREEK";
        }

        if(nsw.length()==1){
            if(Character.isUpperCase(nsw.charAt(0))){
                if((!nsw.equals("P"))||(!nsw.equals("Q"))){
                    return "SEQN";
                }
            }
        }
        double entroAswd = llmAswd.getEntropy(nsw);
        double entroSeqn = llmSeqn.getEntropy(nsw);
        double entroExpn = llmExnp.getEntropy(nsw);

        double isAswd = entroAswd * pAswd ;
        double isExpn = entroExpn * pExpn ;
        double isSeqn = entroSeqn * pSeqn ;
        
//                double isAswd = entroAswd  ;
//        double isExpn = entroExpn ;
//        double isSeqn = entroSeqn  ;

        String tag = max(isAswd, isExpn, isSeqn);

        return tag;

    }

    public String max(double isAswd, double isExpn, double isSeqn) {
        if (isAswd > isExpn) {
            if (isAswd > isSeqn) {
                return "ASWD";
            } else {
                return "SEQN";
            }


        } else {
            if (isExpn > isSeqn) {
                return "EXPN";
            } else {
                return "SEQN";
            }
        }

    }
    
    public void setWordAr() throws UnsupportedEncodingException, FileNotFoundException, IOException {
        BufferedReader bra = new BufferedReader(new InputStreamReader(new FileInputStream("resources\\amtiet.txt"), "UTF16"));
        String amtiet;

        while ((amtiet = bra.readLine()) != null) {
            wordAr.add(amtiet);
        }
    }
    
    public void setGreek() throws UnsupportedEncodingException, FileNotFoundException, IOException {
//        greekAr.add("I");
//        greekAr.add("II");
//        greekAr.add("III");
//        greekAr.add("IV");
//        greekAr.add("V");
//        greekAr.add("VI");
//        greekAr.add("VII");
//        greekAr.add("VIII");
//        greekAr.add("IX");
//        greekAr.add("X");
//        greekAr.add("XI");
        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("resources\\greekNumber.db"), "UTF8"));
        String greekNumber;
        int i = 0;
        while ((greekNumber = br.readLine()) != null) {
            greekAr.add(greekNumber);
//            i++;
        }
    }
        public boolean isWord(String nsw) {
        if (wordAr.contains(nsw)) {
            return true;
        }
//        if(nsw.equals("'")){
//            return true;
//        }
        return false;
    }

    public boolean isGreekNumber(String nsw) {
        if (greekAr.contains(nsw)) {
            return true;
        }
        return false;
    }

    
    
        
}
